using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Schema;
using System.Windows.Forms;

namespace ISYBAUSchemaExporter
{
    public class ISYBAUSchemaParser
    {
        private string mSchemaFilePath;
        private IVisitor mVisitor;

        public ISYBAUSchemaParser(string filePath, IVisitor visitor)
        {
            mSchemaFilePath = filePath;
            mVisitor = visitor;
        }

        public bool Parse()
        {
            XmlSchemaSet schemaSet = new XmlSchemaSet();
            schemaSet.ValidationEventHandler += new ValidationEventHandler(schemaSet_ValidationEventHandler);
            schemaSet.Add(null, mSchemaFilePath);
            schemaSet.Compile();
            foreach (XmlSchema schema in schemaSet.Schemas())
            {
                mVisitor.Visit(schema, null);
            }
            return true;
        }

        private void schemaSet_ValidationEventHandler(object sender, ValidationEventArgs e)
        {
            string message = string.Empty;
            if (e.Severity == XmlSeverityType.Warning)
                message += "Warnning:";
            else if (e.Severity == XmlSeverityType.Error)
                message += "Error:";
            message += e.Message;
            MessageBox.Show(message);
        }
    }
}
